﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Configuration;
using Logging;

namespace ENICHIUSURA
{
    class DictionarySerializer<TKey,TValue>
    {
        //dictionaries can t be serialized so we use a list
        List<DataItem<TKey, TValue>> tempdataitems;

        public DictionarySerializer(Dictionary<TKey, TValue> dict)
        {
            AddDictToList(dict);
        }

        void AddDictToList(Dictionary<TKey, TValue> dict)
        {
            tempdataitems = new List<DataItem<TKey, TValue>>(dict.Count);
            foreach(TKey key in dict.Keys)
                tempdataitems.Add(new DataItem<TKey,TValue>(key,dict[key]));
        }

        public void Serialize()
        {
            string serPath = ConfigurationManager.AppSettings["SerializedDictPath"];
            if (!File.Exists(serPath))
            {
                FileInfo file = new FileInfo(serPath);
                file.Directory.Create();
                File.Create(serPath);
            }
            using (var target = new FileStream(serPath, FileMode.OpenOrCreate))
            {
                var bin = new BinaryFormatter();
                try
                {
                    bin.Serialize(target, tempdataitems);
                }
                catch (Exception e)
                {
                    Logger.LogInfo(e.ToString());
                }
            }
        }
    }

    class DictionaryDeserialize<TKey,TValue>
    {
        public static Dictionary<TKey,TValue> Deserialize()
        {
            string serPath = ConfigurationManager.AppSettings["SerializedDictPath"];
            using (var ms = new FileStream(serPath, FileMode.OpenOrCreate))
            {
                var ser = new BinaryFormatter();
                ms.Position = 0;
                List<DataItem<TKey,TValue>> clone = (List<DataItem<TKey,TValue>>)ser.Deserialize(ms);
                return List2Dict(clone);
            }
        }

        private static Dictionary<TKey, TValue> List2Dict(List<DataItem<TKey, TValue>> clone)
        {
         Dictionary<TKey, TValue> dict = new Dictionary<TKey,TValue>();
         TKey key;
         for (int i = 0; i < clone.Count(); i += 1)
         {
             DataItem<TKey,TValue> currentFile = clone.ElementAt(i);
             key = currentFile.Key; //the directory name serves as key == cod_vel

                 if (!dict.ContainsKey(key))
                 {
                     dict.Add(key, currentFile.Value);
                 }
                 else //already exists the key add to its collection
                 {
                     //List<FileInfo> currentColl = dict[key];
                     //currentColl.Add(currentFile.Value);
                     //dict[key] = currentColl;
                 }
         }
         return dict;

        }
    }
    
    [Serializable]
    class DataItem<TKey,TValue>
    {
        public TValue Value;
        public TKey Key;
        public DataItem(TKey key, TValue value)
        {
            Value = value;
            Key = key;
        }
    }
}
